package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {

    private static final String REGISTER_FORM = "/WEB-INF/jsp/account/register.jsp";
    private String username;
    private String password;
    private String firstname;
    private String lastname;
    private String email;
    private String phone;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zip;
    private String country;
    private String repeatPassword;
    private String msg;
    private String favourite;
    private String language;
    private String listOption;
    private String bannerOption;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username= req.getParameter("username");
        this.password= req.getParameter("password");
        this.firstname= req.getParameter("firstname");
        this.repeatPassword= req.getParameter("repeatedPassword");
        this.lastname= req.getParameter("lastname");
        this.email= req.getParameter("email");
        this.phone= req.getParameter("phone");
        this.address1= req.getParameter("address1");
        this.address2=req.getParameter("address2");
        this.city= req.getParameter("city");
        this.state= req.getParameter("state");
        this.zip= req.getParameter("zip");
        this.country= req.getParameter("country");
        this.favourite= req.getParameter("favourite");
        this.language= req.getParameter("language");

        if(!this.validate()){
            req.setAttribute("msg",this.msg);
            req.getRequestDispatcher("registerForm").forward(req,resp);
        }
        else {
            Account registerAccount = new Account();
            registerAccount.setUsername(this.username);
            registerAccount.setPassword(this.password);
            registerAccount.setFirstName(this.firstname);
            registerAccount.setLastName(this.lastname);
            registerAccount.setEmail(this.email);
            registerAccount.setPhone(this.phone);
            registerAccount.setAddress1(this.address1);
            registerAccount.setAddress2(this.address2);
            registerAccount.setCity(this.city);
            registerAccount.setState(this.state);
            registerAccount.setZip(this.zip);
            registerAccount.setFavouriteCategoryId(this.favourite);
            registerAccount.setLanguagePreference(this.language);
            registerAccount.setCountry(this.country);
            registerAccount.setBannerOption(true);
            registerAccount.setListOption(true);
            registerAccount.setStatus("OK");
            HttpSession session = req.getSession();

            AccountService accountService = new AccountService();
            if(accountService.addUser(registerAccount))
            {
                resp.sendRedirect("MainForm");
            }
            else
            {
                req.setAttribute("msg",accountService.getMsg());
                req.getRequestDispatcher("registerForm").forward(req,resp);
            }
        }
    }
    private boolean validate(){
        if(this.username == null|| this.username.equals("")){
            this.msg = "Username is empty!";
            return false;
        }
        if(this.password == null|| this.password.equals("")){
            this.msg = "Password is empty!";
            return false;
        }
        if(this.repeatPassword == null|| this.repeatPassword.equals("")){
            this.msg = "RepeatPassword is empty!";
            return false;
        }
        if(!this.password.equals(this.repeatPassword)){
            this.msg = "Password is different!";
            return false;
        }
        if(this.phone == null || this.phone.equals("")){
            this.msg = "Phone is empty!";
            return false;
        }
        if(!this.phone.matches("\\d{8,11}")){
            this.msg = "Phone is illeague!";
            return false;
        }
        return true;
    }
}
